package org.csu.assemblyroom.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.List;
import org.csu.assemblyroom.common.CommonResponse;
import org.csu.assemblyroom.entity.Booking;
import org.csu.assemblyroom.persistence.BookingMapper;
import org.csu.assemblyroom.persistence.RoomMapper;
import org.csu.assemblyroom.persistence.UserMapper;
import org.csu.assemblyroom.service.BookingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BookingServiceImpl implements BookingService {
    @Autowired
    private BookingMapper bookingMapper;
    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private UserMapper userMapper;

    //添加预约信息
    @Override
    public CommonResponse<Booking> insertBooking(Booking booking) {
        Booking booking1 = new Booking();
        booking1.setBookid(booking.getBookid());
        booking1.setUserid(booking.getUserid());
        booking1.setRoomnumber(booking.getRoomnumber());
        booking1.setDate(booking.getDate());
        booking1.setDuration(booking.getDuration());

        if(userMapper.selectById(booking1.getUserid()) == null){
            return CommonResponse.createForError("该用户不存在！ 查看所有用户：http://localhost:8080/user/searchAll");
        }else if(roomMapper.selectById(booking1.getRoomnumber()) == null){
            return CommonResponse.createForError("该房间不存在！查看所有房间：http://localhost:8080/room/getAllroom");
        }
        bookingMapper.insert(booking1);
        return CommonResponse.createForSuccess("预约成功！\n查看当前预约信息：http://localhost:8080/booking/"+booking1.getBookid()+
                "\n查看所有预约信息：http://localhost:8080/booking",booking1);
    }

    //通过用户获取相关预约信息
    @Override
    public CommonResponse<List<Booking>> getBookingByUser(String userId) {
        if(userMapper.selectById(userId) == null){
            return CommonResponse.createForError("该用户不存在！ 查看所有用户：http://localhost:8080/user/searchAll");
        }
        QueryWrapper<Booking> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid",userId);
        List<Booking> bookingList = bookingMapper.selectList(queryWrapper);
        if(bookingList.isEmpty()){
            return CommonResponse.createForSuccessMessage("该用户暂无预约信息！查看所有预约信息：http://localhost:8080/booking");
        }else {
            return CommonResponse.createForSuccess(bookingList);
        }
    }

    //通过房间获取相关预约信息
    @Override
    public CommonResponse<List<Booking>> getBookingByRoom(String roomNum) {
        if(roomMapper.selectById(roomNum) == null){
            return CommonResponse.createForError("该房间不存在！查看所有房间：http://localhost:8080/room/getAllroom");
        }
        QueryWrapper<Booking> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("roomnumber",roomNum);
        List<Booking> bookingList = bookingMapper.selectList(queryWrapper);
        if(bookingList.isEmpty()){
            return CommonResponse.createForSuccessMessage("该房间暂无预约信息！查看所有预约信息：http://localhost:8080/booking");
        }else {
            return CommonResponse.createForSuccess(bookingList);
        }
    }

    //通过预约号获取预约信息
    @Override
    public CommonResponse<Booking> getBookingByBookingId(int bookingId) {
        Booking booking = bookingMapper.selectById(bookingId);
        if(booking != null){
            return CommonResponse.createForSuccess("查询预约信息成功! 查看所有预约信息：http://localhost:8080/booking",booking);
        }else {
            return CommonResponse.createForError("查询失败! 查看所有预约信息：http://localhost:8080/booking");
        }
    }

    //获取所有预约信息
    @Override
    public CommonResponse<List<Booking>> getBookingList() {
        List<Booking> bookingList = bookingMapper.selectList(null);
        if(bookingList.isEmpty()){
            return CommonResponse.createForSuccessMessage("暂无预约信息！添加预约信息：http://localhost:8080/booking");
        }
        return CommonResponse.createForSuccess(bookingList);
    }

    //更改预约信息
    @Override
    public CommonResponse<Booking> updateBooking(Booking booking) {
        Booking booking1 = bookingMapper.selectById(booking.getBookid());
        if(booking1 == null){
            return  CommonResponse.createForError("该预约信息不存在！查看所有预约信息：http://localhost:8080/booking");
        }

        Booking booking2 = new Booking();
        booking2.setBookid(booking.getBookid());
        booking2.setUserid(booking.getUserid());
        booking2.setRoomnumber(booking.getRoomnumber());
        booking2.setDate(booking.getDate());
        booking2.setDuration(booking.getDuration());

        QueryWrapper<Booking> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("bookid",booking.getBookid());
        bookingMapper.update(booking2,queryWrapper);
        return CommonResponse.createForSuccess("预约信息更新成功！查看更新后预约信息：http://localhost:8080/booking/"+booking2.getBookid()+
                "\n查看所有预约信息：http://localhost:8080/booking",booking);
    }

    //删除预约信息
    @Override
    public CommonResponse<Booking> deleteBooking(int bookingId) {
        int n = bookingMapper.deleteById(bookingId);
        if(n > 0){
            return CommonResponse.createForSuccessMessage("预约信息删除成功！查看所有预约信息：http://localhost:8080/booking");
        }else{
            return CommonResponse.createForError("删除失败！查看所有预约信息：http://localhost:8080/booking");
        }
    }
}
